package com.dongdongshop.index;

import com.dongdongshop.configresult.Result;
import com.dongdongshop.em.ResultEnum;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class IndexController {

    @RequestMapping("index")
    public String index(){
        return "index";
    }


//    登录
    @RequestMapping("login")
    public String login(){
        return "login";
    }

//    登录逻辑
    @RequestMapping("userController/login")
    public String  login(String username, String password, Model m){

//      借助shiro登录
//         创建subject
        Subject subject = SecurityUtils.getSubject();
//        封装token
        UsernamePasswordToken token = new UsernamePasswordToken(username,password);
        try {
            subject.login(token);
        }catch (UnknownAccountException a){
            m.addAttribute("msg","账号错误");
            return "/login";
        }catch (IncorrectCredentialsException b){
            m.addAttribute("msg","密码错误");
            return "/login";
        }
        return "redirect:/index";
    }


//    注册
    @RequestMapping("register")
    public String register(){

        return "register";
    }

    @RequestMapping("getOr")
    public String getOr(){
        return "cart";
    }

//    我的东东商城
    @RequestMapping("home")
    public String home(){
        return "/home-index";
    }

//    我的东东商城详情页面
    @RequestMapping("homesettingaddress")
    public String homesettingaddress(){

        return "/home-setting-address";
    }

}
